<?php
/*
 * Created on Jun 25, 2007
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
ob_start();
session_start();

require_once("../configuration.php");
require_once("../includes/db/dbsetup.php");
require_once("model/user.class.php");


 $log = null;
 $pwd = null;
 $rememberme = null;
 
 @$rememberme = $_POST['rememberme'];

	unset($_SESSION['u_id']);
	unset($_SESSION['u_login']);
	unset($_SESSION['u_name']);
	unset($_SESSION['err']);
	unset($_SESSION['u_isadmin']);
	unset($_SESSION['file']);
	
	if($_POST['user_login'] != "" && $_POST['user_pass'] != "")
	{
		$user = user::getUserByUserName($db, $_POST['user_login']);
		
		if($user != null)
		{
			$passCrypted = $user->user_pass;
			if(crypt($_POST['user_pass'], $passCrypted) == $passCrypted)
			{
				$_SESSION['u_id'] 		= $user->ID;
				$_SESSION['u_login'] 	= $user->user_login;
				$_SESSION['u_name'] 	= $user->user_login;
				$_SESSION['u_isadmin'] 	= $user->is_admin;
				
				//include this file to set locale ID to SESSION variable
				include_once("../localization.php");
				
				if($rememberme == "forever")
				{
					$expire = time()+60*60*24*30;
					setcookie("ntp[u_id]", $user->ID, $expire);
					setcookie("ntp[u_login]", $user->user_login, $expire);
					setcookie("ntp[u_name]", $user->user_login, $expire);
					setcookie("ntp[u_isadmin]", $user->is_admin, $expire);
				}
				else
				{
					setcookie("ntp[u_id]", "");
					setcookie("ntp[u_login]", "");
					setcookie("ntp[u_name]", "");
					setcookie("ntp[u_isadmin]", "");
				}
				
				
			}
			else
			{
				$_SESSION['err'] = _("Invalid username or password.");
			}
		}
		else
		{
			$_SESSION['err'] = _("Invalid username or password.");
		}
	}
	else
	{
		if($_POST['user_login'] == "" && $_POST['user_pass'] != "")
		{
			$_SESSION['err'] = _("Please type the username.");
		}
		else if($_POST['user_login'] != "" && $_POST['user_pass'] == "")
		{
			$_SESSION['err'] = _("Please type the password.");
		}
		else
		{
			$_SESSION['err'] = _("Please type the username and password.");
		}
	}
	
	//Redirect to desire page
	if($_POST['redirect_to'] == "")
	{
		ob_clean();
		header("Location: ./");
	}
	else
	{
		ob_clean();
		header("Location: ./?" . $_POST['redirect_to']);
	}
		 
ob_end_flush();
?>
